package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.common.Result;
import com.example.demo.entity.StAnswerRecord;
import com.example.demo.entity.StQuestion;
import com.example.demo.entity.StUserExamRecord;
import com.example.demo.entity.pojo.Answer;
import com.example.demo.entity.request.StExamRecordRequest;
import com.example.demo.entity.response.StExamRecordResponse;
import com.example.demo.mapper.StAnswerRecordMapper;
import com.example.demo.mapper.StExamQuestionMapper;
import com.example.demo.mapper.StQuestionMapper;
import com.example.demo.mapper.StUserExamRecordMapper;
import com.example.demo.utils.DateTransferUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/examRecord")
public class StExamRecordController {
    @Resource
    StUserExamRecordMapper examRecordMapper;
    @Resource
    StAnswerRecordMapper answerRecordMapper;
    @Resource
    StQuestionMapper questionMapper;

    @PostMapping("/save")
    public Result<?> save(@RequestBody StExamRecordRequest record) {
        StUserExamRecord examRecord = new StUserExamRecord();
        BeanUtils.copyProperties(record, examRecord);
        examRecord.setSubmitTime(DateTransferUtil.getNow());
        examRecordMapper.insert(examRecord);

        List<Answer> answers = record.getAnswers();
        for (Answer item : answers) {
            StAnswerRecord answerRecord = new StAnswerRecord();
            answerRecord.setRecordId(examRecord.getId());
            answerRecord.setQuestionId(item.getId());
            answerRecord.setContent(item.getContent());

            StQuestion question = questionMapper.selectById(answerRecord.getQuestionId());
            if (question.getType() == 1 || question.getType() == 2 || question.getType() == 5) {
                if (question.getCorrect().equals(answerRecord.getContent())) {
                    answerRecord.setIsCorrect(true);
                } else {
                    answerRecord.setIsCorrect(false);
                }
            }

            answerRecordMapper.insert(answerRecord);
        }

        return Result.success();
    }

    @GetMapping("/select/{id}")
    public Result<?> select(@PathVariable Integer id) {
        StUserExamRecord record = examRecordMapper.selectById(id);
        if (record == null) {
            return Result.error("404", "找不到考试记录");
        }

        StExamRecordResponse response = new StExamRecordResponse();
        BeanUtils.copyProperties(record, response);

        LambdaQueryWrapper<StAnswerRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StAnswerRecord::getRecordId, record.getId());
        List<StAnswerRecord> list = answerRecordMapper.selectList(wrapper);
        response.setAnswers(list);

        return Result.success(response);
    }

    @ApiOperation(value = "修改记录（只能改分数）")
    @PostMapping("/edit")
    public Result<?> edit(@RequestParam Integer id,
                          @RequestParam Integer score) {
        StUserExamRecord record = examRecordMapper.selectById(id);
        if (record == null) {
            return Result.error("404", "找不到考试记录");
        }
        record.setScore(score);
        return Result.success(examRecordMapper.updateById(record));
    }
}
